<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>C语言基础 - 输入输出</title>
    <link rel="stylesheet" href="common.css">
    <style>
        /* 页面特有样式 */
        .format-specifier {
            background-color: #f0f7ff;
            border-radius: 5px;
            padding: 15px;
            margin-bottom: 20px;
        }
        
        .io-table {
            width: 100%;
            margin: 15px 0;
            border-collapse: collapse;
        }
        
        .warning-box {
            background-color: #fff3e0;
            border-left: 4px solid #ff9800;
            padding: 12px;
            margin: 15px 0;
        }
        
        .example-box {
            background-color: #f5f5f5;
            padding: 15px;
            border-radius: 5px;
            margin: 15px 0;
        }
        
        .tips-box {
            background-color: #e8f5e9;
            padding: 15px;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>C语言基础入门课程</h1>
        </header>
        
        <nav>
            <a href="index.html">首页</a>
            <a href="02-intro.html">C语言简介与环境搭建</a>
            <a href="03-structure.html" class="current">C程序结构</a>
            <a href="04-datatypes.html">基本数据类型</a>
            <a href="05-variables.html">变量与常量</a>
            <a href="06-operators.html">运算符</a>
            <a href="07-io.html" class="current">输入输出</a>
            <a href="08-conditionals.html">条件语句</a>
            <a href="09-loops.html">循环结构</a>
            <a href="10-functions.html" class="current">函数基础</a>
            <a href="11-scope.html">作用域与存储类</a>
            <a href="12-recursion.html">递归函数</a>
            <a href="13-arrays.html">一维数组</a>
            <a href="14-multidim.html" class="current">多维数组</a>
            <a href="15-strings.html">字符串处理</a>
            <a href="16-pointers.html">指针基础</a>
            <a href="17-pointers_advanced.html" class="current">指针进阶</a>
            <a href="18-structs.html">结构体</a>
            <a href="19-files.html">文件操作</a>
            <a href="20-project.html">综合项目</a>
        </nav>
        
        <main>
            <h2>C语言输入输出</h2>
            
            <section>
                <h3>printf格式控制</h3>
                
                <div class="format-specifier">
                    <h4>格式说明符</h4>
                    <table class="io-table">
                        <tr>
                            <th>格式符</th>
                            <th>用途</th>
                            <th>示例</th>
                            <th>输出</th>
                        </tr>
                        <tr>
                            <td>%d</td>
                            <td>十进制整数</td>
                            <td>printf("%d", 123);</td>
                            <td>123</td>
                        </tr>
                        <tr>
                            <td>%f</td>
                            <td>浮点数</td>
                            <td>printf("%f", 3.14);</td>
                            <td>3.140000</td>
                        </tr>
                        <tr>
                            <td>%c</td>
                            <td>单个字符</td>
                            <td>printf("%c", 'A');</td>
                            <td>A</td>
                        </tr>
                        <tr>
                            <td>%s</td>
                            <td>字符串</td>
                            <td>printf("%s", "Hello");</td>
                            <td>Hello</td>
                        </tr>
                        <tr>
                            <td>%x</td>
                            <td>十六进制整数</td>
                            <td>printf("%x", 255);</td>
                            <td>ff</td>
                        </tr>
                        <tr>
                            <td>%o</td>
                            <td>八进制整数</td>
                            <td>printf("%o", 8);</td>
                            <td>10</td>
                        </tr>
                        <tr>
                            <td>%e</td>
                            <td>科学计数法</td>
                            <td>printf("%e", 123.456);</td>
                            <td>1.234560e+02</td>
                        </tr>
                        <tr>
                            <td>%%</td>
                            <td>百分号</td>
                            <td>printf("%%");</td>
                            <td>%</td>
                        </tr>
                    </table>
                </div>
                
                <div class="format-specifier">
                    <h4>格式修饰符</h4>
                    <table class="io-table">
                        <tr>
                            <th>修饰符</th>
                            <th>用途</th>
                            <th>示例</th>
                            <th>输出</th>
                        </tr>
                        <tr>
                            <td>%5d</td>
                            <td>最小宽度5</td>
                            <td>printf("%5d", 123);</td>
                            <td>&nbsp;&nbsp;123</td>
                        </tr>
                        <tr>
                            <td>%-5d</td>
                            <td>左对齐</td>
                            <td>printf("%-5d", 123);</td>
                            <td>123&nbsp;&nbsp;</td>
                        </tr>
                        <tr>
                            <td>%05d</td>
                            <td>前导零</td>
                            <td>printf("%05d", 123);</td>
                            <td>00123</td>
                        </tr>
                        <tr>
                            <td>%.2f</td>
                            <td>小数点后2位</td>
                            <td>printf("%.2f", 3.14159);</td>
                            <td>3.14</td>
                        </tr>
                        <tr>
                            <td>%10.2f</td>
                            <td>宽度10，小数点后2位</td>
                            <td>printf("%10.2f", 3.14159);</td>
                            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.14</td>
                        </tr>
                    </table>
                </div>
                
                <div class="example-box">
                    <h4>printf综合示例</h4>
                    <pre><code>#include &lt;stdio.h&gt;

int main() {
    int age = 25;
    float height = 1.75f;
    char grade = 'A';
    
    printf("年龄: %d岁\n", age);
    printf("身高: %.2f米\n", height);
    printf("等级: %c\n", grade);
    printf("百分比: %.1f%%\n", 85.5f);
    
    return 0;
}</code></pre>
                </div>
            </section>
            
            <section>
                <h3>scanf使用技巧</h3>
                
                <div class="warning-box">
                    <h4>scanf基本语法</h4>
                    <pre><code>scanf("格式字符串", &amp;变量1, &amp;变量2, ...);</code></pre>
                    <p><strong>注意：</strong> 必须使用&amp;取地址运算符(字符串数组除外)</p>
                </div>
                
                <div class="tips-box">
                    <h4>scanf格式说明符</h4>
                    <table class="io-table">
                        <tr>
                            <th>格式符</th>
                            <th>用途</th>
                            <th>示例</th>
                        </tr>
                        <tr>
                            <td>%d</td>
                            <td>读取整数</td>
                            <td>scanf("%d", &amp;num);</td>
                        </tr>
                        <tr>
                            <td>%f</td>
                            <td>读取浮点数</td>
                            <td>scanf("%f", &amp;price);</td>
                        </tr>
                        <tr>
                            <td>%lf</td>
                            <td>读取double</td>
                            <td>scanf("%lf", &amp;value);</td>
                        </tr>
                        <tr>
                            <td>%c</td>
                            <td>读取字符</td>
                            <td>scanf("%c", &amp;ch);</td>
                        </tr>
                        <tr>
                            <td>%s</td>
                            <td>读取字符串</td>
                            <td>scanf("%s", str);</td>
                        </tr>
                        <tr>
                            <td>%[^\n]</td>
                            <td>读取整行(包含空格)</td>
                            <td>scanf("%[^\n]", line);</td>
                        </tr>
                    </table>
                </div>
                
                <div class="example-box">
                    <h4>scanf使用示例</h4>
                    <pre><code>#include &lt;stdio.h&gt;

int main() {
    int age;
    float height;
    char name[50];
    
    printf("请输入您的年龄、身高和姓名：");
    scanf("%d %f %s", &amp;age, &amp;height, name);
    
    printf("您好，%s！您今年%d岁，身高%.2f米。\n", name, age, height);
    
    return 0;
}</code></pre>
                </div>
                
                <div class="tips-box">
                    <h4>scanf高级技巧</h4>
                    <ul>
                        <li>使用空格分隔多个输入值</li>
                        <li>在%c前加空格跳过空白字符：<code>scanf(" %c", &amp;ch);</code></li>
                        <li>限制字符串长度防止溢出：<code>scanf("%49s", name);</code></li>
                        <li>检查返回值确保输入成功：<code>if(scanf("%d", &amp;num) != 1) { /* 错误处理 */ }</code></li>
                    </ul>
                </div>
            </section>
            
            <section>
                <h3>常见I/O问题</h3>
                
                <div class="warning-box">
                    <h4>1. 缓冲区问题</h4>
                    <p>混合使用scanf和getchar()时可能出现问题：</p>
                    <pre><code>int num;
char ch;
scanf("%d", &amp;num);  // 输入数字后按回车
ch = getchar();     // 会读取之前输入的回车符</code></pre>
                    <p><strong>解决方案：</strong> 使用<code>while(getchar() != '\n');</code>清空缓冲区</p>
                </div>
                
                <div class="warning-box">
                    <h4>2. 输入类型不匹配</h4>
                    <p>当输入与格式说明符不匹配时，scanf会失败并留下无效数据在缓冲区中。</p>
                    <p><strong>解决方案：</strong> 检查scanf返回值并清空缓冲区</p>
                    <pre><code>int num;
while(1) {
    printf("请输入一个整数：");
    if(scanf("%d", &amp;num) == 1) break;
    while(getchar() != '\n'); // 清空错误输入
    printf("输入无效，请重试！\n");
}</code></pre>
                </div>
                
                <div class="warning-box">
                    <h4>3. 字符串输入安全问题</h4>
                    <p>使用%s读取字符串可能导致缓冲区溢出。</p>
                    <p><strong>解决方案：</strong></p>
                    <ul>
                        <li>限制输入长度：<code>scanf("%9s", str); // 最多读取9个字符</code></li>
                        <li>使用fgets代替：<code>fgets(str, sizeof(str), stdin);</code></li>
                    </ul>
                </div>
                
                <div class="tips-box">
                    <h4>I/O最佳实践</h4>
                    <ul>
                        <li>总是检查scanf的返回值</li>
                        <li>对用户输入进行验证</li>
                        <li>使用fgets和sscanf组合更安全</li>
                        <li>输出时添加适当的提示信息</li>
                        <li>格式化输出提高可读性</li>
                    </ul>
                </div>
            </section>
        </main>
        

</body>
</html>